﻿@{
    ViewBag.CurrentMenu = "Department";
    Layout = "_Layout";
}
<div class="layui-row">
    <table id="departTable" class="layui-table" lay-filter="departTable" ></table>
</div>
<script type="text/javascript">
    var cols = "";
    layui.use(['table', 'jquery', 'treetable','form'],
        function () {
            var $ = layui.jquery;
            var table = layui.table;
            var treetable = layui.treetable;
            var form = layui.form;

            var loadCols = function () {
                $.ajax({
                    url: '@Url.Action("GetDepartmentTableCols", "department")',
                    type: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.success) {
                            cols = [res.data];
                            loadTables();
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
            };
            loadCols();
            //渲染表格
            var loadTables = function () {
                //var formData = $("#searchForm").serializeArray();
                //var tempObj = [];
                //for (var i = 0; i < formData.length; i++) {
                //    var temp = { 'field': '', 'conditional': 'Like', 'value': '' };
                //    temp.field = formData[i].name;
                //    temp.value = formData[i].value;
                //    tempObj.push(temp);
                //}
                treetable.render({
                    elem: '#departTable',
                    id: 'departTable',
                    url: "/Department/GetDepartTreeTable?_t=" + new Date().getTime(),
                    page: false, //分页
                    loading: true,
                    treeColIndex: 0,          // 树形图标显示在第几列
                    treeSpid: '00000000-0000-0000-0000-000000000000',// 最上级的父级id
                    treeIdName: 'id',       // id字段的名称
                    treePidName: 'parentId',     // pid字段的名称
                    treeDefaultClose: false,   // 是否默认折叠
                    treeLinkage: true,        // 父级展开时是否自动展开所有子级
                    height: 'full-150',
                    toolbar: '#toolbarButton',
                    cols: cols
                   // ,where: { searchParam: JSON.stringify(tempObj) }
                });
            };

            //监听工具条
            table.on('tool(departTable)', function (obj) {
                var data = obj.data; //获得当前行数据
                switch (obj.event) {
                    case 'addDepart':
                        top.layer.open({
                            title: '新增',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "Department")?pid=' + data.id,
                            end: function () {
                                loadTables();
                            }
                        });
                        break;
                    case 'editDepart':
                        if (data === undefined||data.id==='') {
                            admin_warning("请选择一行数据");
                            return false;
                        }
                        top.layer.open({
                            title: '编辑',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "Department")?id=' + data.id,
                            end: function () {
                                loadTables();
                            }
                        });
                        break;
                    case 'delDepart':
                         top.layer.confirm("将同时删除下级部门,确认删除?",
                            {
                                btn: ["确定", "取消"]
                            },
                            function () {
                                var sendData = { "id": data.id };
                                $.ajax({
                                    url: '@Url.Action("Delete", "Department")',
                                    type: 'post',
                                    data: sendData,
                                    dataType: 'json',
                                    success: function (res) {
                                        if (res.success) {
                                            top.layer.msg(res.msg, { icon: 1, offset: '15px' });
                                            loadTables();
                                        } else {
                                            top.layer.msg(res.msg, { icon: 2, offset: '15px' });
                                        }
                                    }
                                });
                            });
                        break;
                };
            });
            //监听事件
            table.on('toolbar(departTable)', function (obj) {
                var pid = '00000000-0000-0000-0000-000000000000';
                switch (obj.event) {
                    case 'addTopDepart':
                       top.layer.open({
                            title: '新增',
                            area: ["600px", "500px"],
                            type: 2,
                            maxmin: true,
                            content: '@Url.Action("EditPage", "Department")?pid=' + pid,
                            end: function () {
                                loadTables();
                            }
                        });
                        break;
                }
            });

            //查询提交
            form.on('submit(btnSubmit)', function (data) {
                loadTables();
                return false;
            });
            //查询提交
            form.on('submit(btnReset)', function (data) {
                loadTables();
                return false;
            });
        });
</script>